package edu.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import edu.domain.User;
import edu.service.UserService;
import edu.support.base.controller.AdminBaseController;
import edu.support.context.Constant;
import edu.support.resp.JsonResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;


@RequestMapping("admin")
@Controller
public class AdminIndexController extends AdminBaseController {

    @Autowired
    private UserService userService;
    @Autowired
    private PasswordEncoder passwordEncoder;

    /**
     * 后台首页
     *
     * @return
     */
    @RequestMapping({"/", "index"})
    public String index() {
        // 跳转到图书管理
        return redirect("/admin/goods/list");
    }


    /**
     * 登录页
     *
     * @return
     */
    @RequestMapping(value = "login", method = RequestMethod.GET)
    public String login() {
        return "admin/login";
    }

    /**
     * 登录
     *
     * @param username
     * @param password
     * @return
     */
    @RequestMapping(value = "login", method = RequestMethod.POST)
    public String login(String username, String password, RedirectAttributes model) {
        QueryWrapper<User> wrapper = new QueryWrapper<User>()
                .eq("username", username)
                .eq("password", passwordEncoder.encode(password));
        User user = userService.getOne(wrapper);
        if (null == user) {
            model.addFlashAttribute("message", "用户名或者密码错误");
            return redirect("/admin/login");
        }
        session.setAttribute(Constant.SESSION_ADMIN, user);
        session.setAttribute("sessionAdminId", user.getId());
        return redirect("/admin/index");
    }

    /**
     * 退出登录
     *
     * @return
     */
    @RequestMapping("logout")
    public String logout() {
        session.removeAttribute(Constant.SESSION_ADMIN);
        return redirect("/admin/login");
    }

    /**
     * 修改密码
     *
     * @param password
     * @param pwd
     * @param pwd2
     * @return
     */
    @RequestMapping("modifyPwd")
    @ResponseBody
    public JsonResponse<String> modifyPwd(String password, String pwd, String pwd2) {
        if (!pwd.equals(pwd2)) {
            return JsonResponse.fail("两次密码不一样");
        }
        if (password.equals(pwd)) {
            return JsonResponse.fail("现密码与原密码相同");
        }
        User user = userService.getById(loginUserId());
        if (!passwordEncoder.matches(password, user.getPassword())) {
            return JsonResponse.fail("原密码不对");
        }
        user.setPassword(passwordEncoder.encode(pwd));
        userService.updateById(user);
        session.removeAttribute(Constant.SESSION_ADMIN);
        return JsonResponse.success("修改成功");
    }
}
